<?php
  session_start(); // start up your PHP session!
  // create short variable names
  $uname = $_POST['uname'];
  $upass = $_POST['upass'];
?>

<html>
<head>
</head>
<body>
<?php
  $fp = fopen('user.cfg', 'r');
  if($fp)
  {
    $arr = array();
    while(!feof($fp))
	{    
      $name = stream_get_line( $fp, 1024, "\r\n" );
      $pass = stream_get_line( $fp, 1024, "\r\n" );
	  $arr[$name] = $pass;
	}
    fclose($fp);

    if($arr[$uname] == $upass)
    {
      $_SESSION['uname'] = $uname;
      $_SESSION['upass'] = $upass;         
    
	  $fp2 = fopen('param.cfg', 'r');
	  if($fp2)
	  {
	    $_SESSION['delay'] = stream_get_line($fp2, 1024, "\n");
		$_SESSION['count'] = stream_get_line($fp2, 1024, "\n");
		fclose($fp2);
	  }
	  else
	  {
	    $_SESSION['delay'] = "Can't open";
		$_SESSION['count'] = "Can't open";
	  }
	  
	  $fp3 = fopen('alert.cfg', 'r');
	  if($fp3)
	  {
	    $_SESSION['long-absence'] = stream_get_line($fp3, 1024, "\n");
      $_SESSION['long-present'] = stream_get_line($fp3, 1024, "\n");
      fclose($fp3);
	  }
	  else
	  {
	    $_SESSION['long-absence'] = "Can't open";
      $_SESSION['long-present'] = "Can't open";
	  }	  
	  
	  	  if($uname == "admin")
	  	  {
          $_SESSION['isAdmin'] = true;          
          header('Location: ModeSelection.php');     	  
	  	  }
	  	  else
	  	  {
          $_SESSION['isAdmin'] = false;
          header('Location: WorkingMode.php');     	  
	  	  }	        
	
    }
    else
    {
      $_SESSION['error'] = "Username or password is incorrect.";
      header('Location: Index.php');
    }
  }
  else
  {
     $_SESSION['error'] = "Cannot access the server.";
     header('Location: Index.php');
  }

?>
</body>
</html>